package com.example.service;

import com.example.common.enums.ModuleEnum;
import com.example.controller.domain.SearchContent;
import com.example.entity.Likes;
import com.example.entity.Messages;
import com.example.entity.Notebook;
import com.example.entity.Works;
import com.example.mapper.LikesMapper;
import com.example.mapper.NotebookMapper;
import com.example.mapper.WorksMapper;
import com.example.utils.SearchRedisHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LikesService {

    @Resource
    private LikesMapper likesMapper;
    @Resource
    private MessagesService messagesService;
    @Resource
    private NotebookMapper notebookMapper;
    @Resource
    private WorksMapper worksMapper;
    @Resource
    private SearchRedisHelper searchRedisHelper;

    /**
     * 1. 点赞
     * 2. 取消点赞
     * 取决于当前的模块数据有没有被点赞
     */
    public void set(Likes likes) {
        List<Likes> likesList = likesMapper.selectLikes(likes);
        if (ObjectUtils.isEmpty(likesList)) {
            likes.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            likes.setDelTag("0");
            //添加标志位
            likesMapper.insert(likes);
            Messages messages=new Messages();
            messages.setFid(likes.getFid());
            messages.setModule(likes.getModule());
            messages.setOptType(ModuleEnum.LIKE.getValue());
            String dynamicName;
            if(messages.getModule().equals(ModuleEnum.NOTEBOOK.getValue())){
                Notebook notebook=notebookMapper.selectById(messages.getFid());
                dynamicName=notebook.getTitle();
                messages.setUserId(notebook.getUserId());
            }else{
                Works works=worksMapper.selectById(messages.getFid());
                dynamicName=works.getName();
                messages.setUserId(works.getUserId());
            }
            messagesService.add(messages,dynamicName);
            SearchContent searchContent=new SearchContent();
            searchContent.setName(dynamicName);
            searchContent.setFid(likes.getFid());
            searchContent.setModule(likes.getModule());
            searchRedisHelper.addRedisHotSearch(searchContent,2L);
        } else {
            likes.setDelTag("1");
           likes.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
            List<Likes> isExist=likesMapper.selectLikes(likes);
            if (!ObjectUtils.isEmpty(isExist)){
                likes.setDelTag("0");
                likesMapper.update(likes);
            }else{
                likesMapper.update(likes);
            }
        }
    }

    public List<Likes> selectLikes(Integer fid, Integer userId, String module,String delTag) {
        Likes likes = new Likes();
        likes.setFid(fid);
        likes.setUserId(userId);
        likes.setModule(module);
        likes.setDelTag(delTag);
        return likesMapper.selectLikes(likes);
    }

    public List<Likes> selectWorksAll() {
        return likesMapper.selectWorksAll();
    }
    public List<Likes> selectNotebookAll() {
        return likesMapper.selectNotebookAll();
    }
}
